import 'package:flutter/material.dart';
import 'package:flutterhello/api/api.dart';
import 'package:flutterhello/controller/Controller.dart';
import 'package:flutterhello/pages/notice/widget/act_card.dart';
import 'package:flutterhello/util/host_util.dart';
import 'package:flutterhello/widgets/my_toast.dart';
import 'package:flutterhello/widgets/tips_card.dart';
import 'package:get/get.dart';

class ActItem extends StatelessWidget {
  final _controller = Get.put(Controller());
  var info;
  ActItem({this.info});
  @override
  Widget build(BuildContext _context) {
    print('------debug------');
    print(info);
    return Padding(
      padding: EdgeInsets.only(left: 15, right: 15, bottom: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            info['createTime'],
            style: TextStyle(color: Colors.black45),
          ),
          SizedBox(
            height: 20,
          ),
          Container(
            decoration: BoxDecoration(
              color: Colors.white,
              boxShadow: [
                BoxShadow(
                    color: Colors.grey[400],
                    offset: Offset(0, 1), // 偏移量
                    blurRadius: 2),
              ],
              borderRadius: BorderRadius.all(
                Radius.circular(10.0),
              ),
            ),
            child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                children: [
                  Row(
                    children: [
                      Container(
                        width: 70,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            info['type'] == 'user' ? Text('用户') : Text('团队')
                          ],
                        ),
                      ),
                      SizedBox(
                        width: 10,
                      ),
                      Expanded(
                        child: TipsCard(
                          favicon:
                              HostUtil.getHost() + this.info['fromFavicon'],
                          name: this.info['fromName'],
                        ),
                      )
                    ],
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  Row(
                    children: [
                      Container(
                        width: 70,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [Text('申请加入')],
                        ),
                      ),
                      SizedBox(
                        width: 10,
                      ),
                      Expanded(
                        child: Container(),
                      )
                    ],
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  ActiviteCard(
                    info: info['activite'],
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  GestureDetector(
                    child: Container(
                      height: 45,
                      width: double.infinity,
                      decoration: BoxDecoration(
                        color: Colors.blueGrey,
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                      ),
                      child: Center(
                        child: Text(
                          '同 意',
                          style: TextStyle(color: Colors.white, fontSize: 18),
                        ),
                      ),
                    ),
                    onTap: () async {
                      var result = await showDialog(
                        context: _context,
                        builder: (context) {
                          return AlertDialog(
                            title: Text('提示'),
                            content: Text('确认同意加入吗？'),
                            actions: <Widget>[
                              TextButton(
                                child: Text('取消'),
                                onPressed: () {
                                  Navigator.of(context).pop('cancel');
                                },
                              ),
                              TextButton(
                                child: Text('确认'),
                                onPressed: () {
                                  submit(
                                      info['fromID'],
                                      info['type'],
                                      info['fromFavicon'],
                                      info['fromName'],
                                      info['activiteID'],
                                      info['id'],
                                      _controller,
                                      _context);
                                },
                              ),
                            ],
                          );
                        },
                      );
                      print('$result');
                    },
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

submit(itemID, type, favicon, name, activiteID, id, Controller controller,
    context) async {
  var res = await Api.joinActivite({
    "itemID": itemID,
    "type": type,
    "favicon": favicon,
    "name": name,
    "activiteID": activiteID,
  });
  print(res);
  if (res['code'] == 1000) {
    var res2 = await Api.deleteActiviteNews({
      "newsID": id,
    });
    print(res2);
    await controller.getActiviteNews();
    Navigator.of(context).pop();
    MyTost.showTost(res['message']);
  } else {
    Navigator.of(context).pop();
    MyTost.showTost(res['message']);
  }
}
